package gzhaoku.com.common.util;

import android.content.Context;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;


import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import gzhaoku.com.machinemain.BuildConfig;


public class Api extends ApiCommon{

    public static boolean testNetwork(JsonHttpResponseHandler handler){
        final String url = ApiHost + "/api/getServerTime.json";
        log.i("[API->]"+url);
        client.get(url, null, handler);
        return true;
    }

    /**
     获取机器密钥 (证书)
     /api/acquireMachineSecret.json

     参数
     machineSn: string - 公共参数。机器编码
     nonce: string - 公共参数。随机字符串，长度 32 字符，5 分钟内有重复则当作不合法
     sign: string - 公共参数。签名
     timestamp: integer - 公共参数。请求发起的 Unix 时间戳（秒），暂时不作时间校验
     返回
     secret: string - 机器密钥
     * @return
     */
    public static boolean acquireMachineSecret(Context context, ApiCallback callback){
        final String url = ApiHost + "/api/acquireAppSecret.json";
        String nonce = UUID.randomUUID().toString().replaceAll("\\-", "");
        long timestamp = System.currentTimeMillis() / 1000;

        Map<String, Object> params = new HashMap();
        params.put("machineSn", Setting.get("machineSn"));
        params.put("appId", BuildConfig.APP_ID);
        params.put("nonce", nonce);
        params.put("timestamp", timestamp);


        RequestParams p = sign(params, Certs.readMachineInstallKey(context));
        log.i("[API->]"+url);
        client.post(url, p, new Handler(callback));
        return true;
    }

    /**
     获取一个新 token
     /api/acquireToken.json

     参数
     machineSn: string - 公共参数。机器编码
     nonce: string - 公共参数。随机字符串，长度 32 字符，5 分钟内有重复则当作不合法
     sign: string - 公共参数。签名
     timestamp: integer - 公共参数。请求发起的 Unix 时间戳（秒），暂时不作时间校验
     返回
     token: string - token
     * @return
     */
    public static boolean acquireToken(ApiCallback callback){
        final String url = ApiHost + "/api/acquireAppToken.json";
        String nonce = UUID.randomUUID().toString().replaceAll("\\-", "");
        long timestamp = System.currentTimeMillis() / 1000;

        Map<String, Object> params = new HashMap();
        params.put("machineSn", Setting.get("machineSn"));
        params.put("appId", BuildConfig.APP_ID);
        params.put("nonce", nonce);
        params.put("timestamp", timestamp);

        RequestParams p = sign(params, Setting.get("secret"));
        log.i("[API->]"+url);
        client.post(url, p, new Handler(callback));
        return true;
    }


    /**
     获取机器广告列表
     /advert/api/listAdvert.json

     RequireToken: true

     参数
     token: string - 公共参数，token
     返回
     adverts: list[object] -
         createTime: string - 创建时间
         durationMs: integer - 播放时长, 单位毫秒
         id: integer - id
         type: integer - type
             0: UNKNOWN
             1: IMAGE
             2: VIDEO
         url: string - 资源路径
         weight: integer - 权重
     * @return
     */
    public static boolean listAds(Context context, ApiCallback callback){
        final String url = ApiHost + "/advert/api/listAdvert.json";

        Map<String, Object> params = new HashMap();
        params.put("token", Setting.get("token"));


        RequestParams p = sign(params, Certs.readMachineInstallKey(context));
        log.i("[API->]"+url);
        client.post(url, p, new Handler(callback));
        return true;
    }

    /**
     获取主应用的待机屏保
     /mainapp/api/getScreensaver.json

     RequireToken: true

     参数
     token: string - 公共参数，token
     返回
     screensavers: list[object] -
     durationMs: integer - 播放时长, 单位毫秒
     id: integer - id
     link: string - 跳转链接
     type: integer - type
     0: UNKNOWN
     1: IMAGE
     2: VIDEO
     url: string - 资源路径
     * @return
     */
    public static boolean screenSavers(Context context, ApiCallback callback){
        final String url = ApiHost + "/mainapp/api/getScreensaver.json";

        Map<String, Object> params = new HashMap();
        params.put("token", Setting.get("token"));


        RequestParams p = sign(params, Certs.readMachineInstallKey(context));
        log.i("[API->]"+url);
        client.post(url, p, new Handler(callback));
        return true;
    }

    /**
     获取主应用的主按钮列表
     /mainapp/api/getMainButton.json

     RequireToken: true

     参数
     token: string - 公共参数，token
     返回
     buttons: list[object] -
     id: integer - id
     imageList: list[string] - 图片链接
     link: string - 跳转链接
     title: string - 标题
     * @return
     */
    public static boolean getMainButton(Context context, ApiCallback callback){
        final String url = ApiHost + "/mainapp/api/getMainButton.json";

        Map<String, Object> params = new HashMap();
        params.put("token", Setting.get("token"));


        RequestParams p = sign(params, Certs.readMachineInstallKey(context));
        log.i("[API->]"+url);
        client.post(url, p, new Handler(callback));
        return true;
    }

    /**
     判断设备是否已激活
     /api/activeStatus.json

     参数
     appId: integer - 公共参数。AppId
     10001: GMALL_MAIN
     10011: GMALL_AD
     machineSn: string - 公共参数。机器编码
     nonce: string - 公共参数。随机字符串，长度 32 字符，5 分钟内有重复则当作不合法
     sign: string - 公共参数。签名
     timestamp: integer - 公共参数。请求发起的 Unix 时间戳（秒），暂时不作时间校验
     返回
     activeStatus: boolean -
     * @return
     */
    public static boolean activeStatus(ApiCallback callback){
        final String url = ApiHost + "/api/activeStatus.json";
        String nonce = UUID.randomUUID().toString().replaceAll("\\-", "");
        long timestamp = System.currentTimeMillis() / 1000;

        Map<String, Object> params = new HashMap();
        params.put("machineSn", Setting.get("machineSn"));
        params.put("appId", BuildConfig.APP_ID);
        params.put("nonce", nonce);
        params.put("timestamp", timestamp);

        RequestParams p = sign(params, Setting.get("secret"));

        log.i("[API->]"+url);
        client.post(url, p, new Handler(callback));
        return true;
    }

    public static boolean checkVersion(Context context, JsonHttpResponseHandler hanlder){
        final String url = BuildConfig.CHECK_URL;
        try{
            RequestParams params = new RequestParams();
            params.put("r", new Random().nextInt());
            log.i("[API->]"+url+'?'+params.toString());
            client.get(url, params, hanlder);
            return true;
        }catch (Exception e){
            log.e(e);
            return false;
        }

    }
}
